/**
 * 项目名称: Topic_96.cpp
 * 项目路径: study/DataStructure/LeetCode/树/中等
 * 编辑时间: 2024年08月26日07时39分55秒
 * 用   户: 轻尘
 */

#include <bits/stdc++.h>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    int numTrees(int n) {
        int denominator = n+1;
        int molecule = 2 * n;

        long sum_m=1;
        long sum_d_1 = 1;
        for(int i = 1;i<=denominator;i++)sum_d_1*=i;

        unordered_set<long>set;
        for(int i = molecule;i>=n+1;i--)
            if(sum_d_1 % i ==0){
                sum_d_1/=i;
                set.insert(i);
            }

        for(long i=n+1;i<=molecule;i++){
            if(set.find(i)!=set.end())continue;
            else sum_m*=i;
        }

        return sum_m/sum_d_1;
    }
};
